package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.bean.AdSpace;
import cn.nubia.admin.application.bean.ServiceConstant;
import cn.nubia.admin.application.dto.AdSpaceDTO;
import cn.nubia.admin.application.query.AdSpaceQuery;
import cn.nubia.admin.application.service.AdSpaceService;
import cn.nubia.admin.application.bean.Terminal;
import cn.nubia.admin.application.service.TerminalService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by Administrator on 2015/7/6.
 */
@Controller
@RequestMapping("/admin/ad")
public class AdSpaceController {


    @Autowired
    private AdSpaceService adSpaceService;


    @Autowired
    private TerminalService terminalService;


    @RequiresPermissions(value = "soft:ad:adSpaceList")
    @RequestMapping(value = "adSpaceList")
    public ModelAndView getAdSpaceList(AdSpaceQuery adSpaceQuery) {
        ModelAndView modelAndView = new ModelAndView("/ad/adSpaceList");
        Paged<AdSpaceDTO> adSpacePage = adSpaceService.findPage(adSpaceQuery);
        modelAndView.addObject("adSpaceList", adSpacePage);
        modelAndView.addObject("query", adSpaceQuery);
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping(value = "saveAdSpace", method = RequestMethod.POST)
    public Response<Boolean> save(AdSpace adSpace) {
        adSpaceService.save(adSpace);
        return Response.getSuccessResponse(true);
    }

    @RequestMapping(value = "viewAdSpace", method = RequestMethod.GET)
    public ModelAndView view(Integer adSpaceId) {
        ModelAndView modelAndView = new ModelAndView("/ad/adSpaceForm");
        AdSpace adSpace = new AdSpace();
        Integer terminalId = null;
        if (adSpaceId != null) {
            adSpace = adSpaceService.get(adSpaceId);
            Terminal t = terminalService.get(adSpace.getTerminalId());
            if (t != null) {
                terminalId = t.getId();
            }
        }
        List<Terminal>  terminalList = terminalService.findAll();
        modelAndView.addObject("terminalList", terminalList);
        modelAndView.addObject("terminalId", terminalId);

        modelAndView.addObject("adSpace", adSpace==null?new AdSpace():adSpace);
        modelAndView.addObject("adSpaceId", adSpaceId);
        modelAndView.addObject("showTypes", ServiceConstant.showTypes);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "deleteAdSpace", method = RequestMethod.GET)
    public Response<Boolean> delete(Integer adSpaceId) {
        adSpaceService.deleteAdSpace(adSpaceId);
        return Response.getSuccessResponse(true);

    }


}
